package com.atguigu.ssyx.product.controller;

import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.product.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @作者:胡树苑
 * @时间:2025/9/13 19:37
 */
@Api(tags = "文件上传接口")
@RestController
@RequestMapping("/admin/product")
@CrossOrigin
public class FileUploadController {
    @Autowired
    private FileUploadService fileUploadService;
    //图片上传的方法
    @ApiOperation("图片上传")
    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile file) {
        try{
            String url= fileUploadService.uploadFile(file);
            return Result.ok(url);
        }catch (Exception e){
            return Result.build(null,406,"文件上传失败");
        }
    }
    //获取url
    @ApiOperation("获取路径")
    @GetMapping("/getUrl")
    public Result getUrl(@RequestParam String newFileName) {
        try {
            String url = fileUploadService.getUrl(newFileName);
            return Result.ok(url);
        }catch (Exception e){
            return Result.build(null,409,"url获取失败");
        }
    }
}
